﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThridGroup.ERP.DTO;
using ThridGroup.ERP.ProductionSystem;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace ThridGroup.ERP.WarehouseManagement
{
    public class SaleService : ApplicationService, SaleIService
    {
        private readonly IRepository<SaleModel, int> _salerepository;
        public SaleService(IRepository<SaleModel, int> salerepository)
        {
            _salerepository = salerepository;
        }

        public async Task<int> AddSale(SaleDTO sale)
        {
            var list = ObjectMapper.Map<SaleDTO, SaleModel>(sale);
            var result = await _salerepository.InsertAsync(list);
            return result == null ? 0:1;
        }

        public async Task<int> DeleteSale(int saleId)
        {
            var Del = _salerepository.DeleteAsync(c => c.Id == saleId);
            return Del == null ? 0 : 1;
        }

        public async Task<SaleDTO> FanSale(int saleId)
        {
            var list = await _salerepository.FindAsync(c=>c.Id == saleId);
            var result = ObjectMapper.Map<SaleModel, SaleDTO>(list);
            return result;
        }

        public async Task<List<SaleDTO>> GetSale(int pageIndex = 1, int pageSize = 10)
        {
            var list = await _salerepository.GetListAsync();
            var result = ObjectMapper.Map<List<SaleModel>, List<SaleDTO>>(list);
            return result;
        }
        
        public async Task<int> UpdateSale(SaleDTO sale)
        {
            var list = ObjectMapper.Map<SaleDTO, SaleModel>(sale);
            var result = await _salerepository.UpdateAsync(list);
            return result == null ? 0 : 1;
        }
    }
}
